<template>
  <div class="container">
    <div class="title">
      <p>农补通系统</p>
    </div>
    <div class="login">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username" class="form-item">
          <div style="display: flex; align-items: center; overflow: hidden;">
            <img style="width: 24px; height: 24px; " src="../assets/images/user.svg" />
            <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="请输入账号">
              <!-- <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" /> -->
            </el-input>
          </div>
        </el-form-item>
        <el-form-item prop="password" class="form-item">
          <div style="display: flex;  align-items: center; overflow: hidden;">
            <img style="width: 24px; height: 24px; " src="../assets/images/password.svg" />
            <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="请输入密码"
              @keyup.enter.native="handleLogin">
            </el-input>
          </div>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled">
          <div style="display: flex;   overflow: hidden; justify-content: space-between;" >
            <div style="display: flex; align-items: center;  width: 60%; overflow: hidden;" class="form-item">
              <img style="width: 24px; height: 24px;" src="../assets/images/validcode.svg" />
              <el-input v-model="loginForm.code" auto-complete="off" placeholder="请输入验证码"
                @keyup.enter.native="handleLogin">
              </el-input>
            </div>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img" />
            </div>
          </div>
        </el-form-item>


        <!-- <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox> -->
        <el-form-item style="width:100%; text-align: center;">
          <el-button :loading="loading" size="medium" type="primary"
            style="width:80%;margin-top: 5%;background-color: rgba(5,239,248,1);color: rgba(6,67,79,1);border:none;font-size: 16px;"
            @click.native.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div style="float: right;" v-if="register">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
        </el-form-item>
      </el-form>
      <!--  底部  -->
      <!-- <div class="el-login-footer">
      <span>Copyright © 2023-2023 nbacheng All Rights Reserved.</span>
    </div> -->
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { getCodeImg } from "@/api/login";
import { authBinding } from "@/api/system/auth";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
import user from '../assets/images/user.svg'; // adjust the path as needed
import password from '../assets/images/password.svg';
import validcode from '../assets/images/validcode.svg';

export default {
  name: "Login",
  components: {
  },
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.data.img;
          this.loginForm.uuid = res.data.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(() => { });
            

          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    },
    
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.container {
  background-image: url("../assets/images/login_bg.png"),url("../assets/images/login_bg2.png");
  // background-color: #1B3948;
  background-size: contain,contain;

  justify-content: center;
  align-items: center;
  background-position: center;
  background-repeat: no-repeat;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

.title {
  width: 100%;
  height: 25%;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  line-height: 58px;
  color: rgba(1, 251, 248, 1);
}

.title p {
  margin: 0;
  /* 去掉默认的margin，防止影响布局 */
  font-size: 48px;
  font-family: Arial-700;
}

.login {
  display: flex;
  justify-content: center;
  align-items: center;
  background-position: center;
  background-repeat: no-repeat;
  width: 100%;
  height: 75%;
  margin-top: 1%;
}

.form-item {
  border-radius: 3px 3px 0px 0px;
  background-color: rgba(6, 67, 79, 1);
  color: rgba(51, 51, 51, 1);
  font-size: 13px;
  text-align: center;
  font-family: -regular;
  border: 2px solid rgba(2, 240, 253, 1);
}

.login-form {
  // background: hsla(0, 28%, 89%, 0.489);
  width: 16vw;
  margin-bottom: 13%;
  height: 35%;
  font-size: 20px;
  .el-input {
    height: 50px;
    font-size: 16px;
    border: none;
    outline: none;

    input {
      height: 50px;
      font-size: 16px;
      border: none;
      outline: none;
      background-color: rgba(255, 127, 80, 0);
    }
  }

  .input-icon {
    height: 50px;
    width: 50px;
    margin-left: 2px;
  }
}



.login-code {
  width: 36%;
  // height: 38px;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

</style>
